public class MySingleList implements IList {
    //节点使用内部类来写
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //定义一个链表的成员的头结点
    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(33);
        ListNode node3 = new ListNode(99);
        ListNode node4 = new ListNode(67);
        ListNode node5 = new ListNode(88);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }


    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        if (head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }

    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            this.head = node;
        } else {
            ListNode cur = head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode prev = this.head;
        if (check(index)) {
            //还要考虑头插
            if (index == 0) {
                addFirst(data);
                return;
            } else if (index == this.size()) {
                addLast(data);
                return;
            } else {
                while (index-- > 1) {
                    prev = prev.next;
                }
                ListNode cur = new ListNode(data);
                cur.next = prev.next;
                prev.next = cur;
            }
        } else {
            return;
        }

    }

    private boolean check(int index) {
        if (index < 0 || index > this.size()) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }

        ListNode cur = findPrev(key);
        if (cur == null) {
            System.out.println("没有要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
        //del = null;
        //因为del是局部变量，会自动销毁
    }

    private ListNode findPrev (int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        //删除左右置为key的节点
        if(this.head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }

        if (head.val == key) {
            head = head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur != null) {
            ListNode nextPos = cur.next;
            //cur.val = null
            //cur.next = null
            cur.next = null;
            cur = nextPos;
        }
        this.head = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode findKthToTail (int k) {
        if (k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while(k - 1 != count) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
            count++;
        }
        while(fast != null && fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
